{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "构造矩阵，但不初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[3.7826e-35, 6.0956e-43, 3.7823e-35],\n",
      "        [6.0956e-43, 3.7827e-35, 6.0956e-43],\n",
      "        [3.7827e-35, 6.0956e-43, 3.7826e-35],\n",
      "        [6.0956e-43, 3.7826e-35, 6.0956e-43],\n",
      "        [3.7826e-35, 6.0956e-43, 3.7823e-35]])\n"
     ]
    }
   ],
   "source": [
    "x = torch.empty(5,3)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "构造随机初始化矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.2245, 0.1212, 0.4250],\n",
      "        [0.2083, 0.4504, 0.8614],\n",
      "        [0.9933, 0.5067, 0.1931],\n",
      "        [0.7266, 0.2928, 0.7806],\n",
      "        [0.1938, 0.3108, 0.6651]])\n"
     ]
    }
   ],
   "source": [
    "x = torch.rand(5,3)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "构造一个全0，数据类型是long的矩阵//"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0, 0, 0],\n",
      "        [0, 0, 0],\n",
      "        [0, 0, 0],\n",
      "        [0, 0, 0],\n",
      "        [0, 0, 0]])\n"
     ]
    }
   ],
   "source": [
    "x = torch.zeros(5,3,dtype = torch.long)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "构造一个张量，直接使用数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([1.5000, 2.0000])\n"
     ]
    }
   ],
   "source": [
    "x = torch.tensor([1.5,2])\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建一个基于已存在tensor的tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1., 1., 1.],\n",
      "        [1., 1., 1.],\n",
      "        [1., 1., 1.],\n",
      "        [1., 1., 1.],\n",
      "        [1., 1., 1.]], dtype=torch.float64)\n",
      "tensor([[ 1.0624, -0.0760, -0.4773],\n",
      "        [-0.1440,  1.1108,  0.8583],\n",
      "        [ 0.8030, -0.7521, -0.7321],\n",
      "        [-0.7389, -1.3217,  0.4097],\n",
      "        [ 0.3275,  0.3938,  0.0795]])\n"
     ]
    }
   ],
   "source": [
    "x = x.new_ones(5,3,dtype = torch.double)\n",
    "print(x)\n",
    "\n",
    "x = torch.randn_like(x, dtype = torch.float)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "获取维度信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([5, 3])\n",
      "row = 5, col = 3\n"
     ]
    }
   ],
   "source": [
    "print(x.size())\n",
    "row, col = x.size() # 返回一个元组\n",
    "print(f'row = {row}, col = {col}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "加法的两种方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 1.2397,  0.0774, -0.0037],\n",
      "        [-0.0915,  1.8356,  1.7391],\n",
      "        [ 1.4990, -0.0796,  0.0361],\n",
      "        [-0.1341, -0.8448,  1.2340],\n",
      "        [ 1.0056,  1.0446,  0.9737]])\n",
      "tensor([[ 1.2397,  0.0774, -0.0037],\n",
      "        [-0.0915,  1.8356,  1.7391],\n",
      "        [ 1.4990, -0.0796,  0.0361],\n",
      "        [-0.1341, -0.8448,  1.2340],\n",
      "        [ 1.0056,  1.0446,  0.9737]])\n"
     ]
    }
   ],
   "source": [
    "y = torch.rand(5,3)\n",
    "print(x + y)\n",
    "print(torch.add(x,y))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "加法：提供一个输出tensor作为参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 1.2397,  0.0774, -0.0037],\n",
      "        [-0.0915,  1.8356,  1.7391],\n",
      "        [ 1.4990, -0.0796,  0.0361],\n",
      "        [-0.1341, -0.8448,  1.2340],\n",
      "        [ 1.0056,  1.0446,  0.9737]])\n"
     ]
    }
   ],
   "source": [
    "result = torch.empty(5,3)\n",
    "torch.add(x, y, out = result)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "加法：in-place"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.1773, 0.1534, 0.4736],\n",
      "        [0.0525, 0.7248, 0.8808],\n",
      "        [0.6960, 0.6725, 0.7682],\n",
      "        [0.6048, 0.4769, 0.8242],\n",
      "        [0.6781, 0.6508, 0.8942]])\n",
      "tensor([[ 1.2397,  0.0774, -0.0037],\n",
      "        [-0.0915,  1.8356,  1.7391],\n",
      "        [ 1.4990, -0.0796,  0.0361],\n",
      "        [-0.1341, -0.8448,  1.2340],\n",
      "        [ 1.0056,  1.0446,  0.9737]])\n"
     ]
    }
   ],
   "source": [
    "print(y)\n",
    "y.add_(x)\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "索引操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([ 1.0624, -0.1440,  0.8030, -0.7389,  0.3275])\n"
     ]
    }
   ],
   "source": [
    "print(x[:,0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "改变大小：torch.view (means view from other aspects)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])\n"
     ]
    }
   ],
   "source": [
    "x = torch.randn(4,4)\n",
    "y = x.view(16)\n",
    "z = x.view(-1,8) # the size -1 is inferred from other dimensions\n",
    "print(x.size(),y.size(),z.size())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果你有一个元素的tensor，使用.item()来获得这个value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([0.7578])\n",
      "0.757847785949707\n"
     ]
    }
   ],
   "source": [
    "x = torch.randn(1)\n",
    "print(x)\n",
    "print(x.item())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
